package top.lionkliu.servlet;

import lombok.SneakyThrows;
import top.lionkliu.entity.Note;
import top.lionkliu.entity.User;
import top.lionkliu.service.impl.NoteServiceImpl;
import top.lionkliu.utils.SystemConstants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet({"/note/list", "/note/delete", "/note/add", "/note/edit", "/note/search"})
public class NoteServlet extends HttpServlet {

    private NoteServiceImpl noteService = new NoteServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        switch (servletPath) {
            case "/note/list":
                doList(request, response);
                break;
            case "/note/delete":
                doDel(request, response);
                break;
            case "/note/add":
                doAdd(request, response);
                break;
            case "/note/edit":
                doModify(request, response);
                break;
        }
    }


    @SneakyThrows
    private void doList(HttpServletRequest request, HttpServletResponse response) {
        List<Note> notes = noteService.selectAll();
        request.getSession().setAttribute("notes", notes);
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        String flag = loginUser.getFlag();
        if (flag.equals(SystemConstants.IS_ADMIN)){
            request.getRequestDispatcher("/admin/note.jsp").forward(request, response);
        }else {
            request.getRequestDispatcher("/user/note.jsp").forward(request, response);
        }
    }

    @SneakyThrows
    private void doDel(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        noteService.delete(id);
        response.sendRedirect(request.getContextPath() + "/note/list");
    }

    @SneakyThrows
    private void doAdd(HttpServletRequest request, HttpServletResponse response) {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        noteService.insert(new Note(title, author, content));
        response.sendRedirect(request.getContextPath() + "/note/list");
    }


    @SneakyThrows
    private void doModify(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String id = (String) session.getAttribute("id");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        Note note = new Note(Integer.parseInt(id), title, author, content);
        noteService.update(note);
        response.sendRedirect(request.getContextPath() + "/note/list");
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
